# -*- coding:utf-8 -*-
import sys
import time
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from DBhandler import ADhandler


class AddWindow(QWidget):

    def __init__(self,btn_index_map):
        super(AddWindow, self).__init__()

        self.btn_index_map = btn_index_map

        self.big_font = QFont('',20,64)
        self.label_font = QFont('',10,75)
        self.btn_style = '''QPushButton{color:black}
                                  QPushButton:hover{color:#FF7F24}
                                  QPushButton{background-color:#CFCFCF}
                                  QPushButton{border:2px}
                                  QPushButton{border-radius:10px}
                                  QPushButton{padding:5px 1px}'''

        self.duration_map = {'1'}

        self.db_hanlder = ADhandler()

        self.main_layout = QVBoxLayout()
        self.main_layout.setSpacing(25)
        self.setWindowTitle("发布任务")
        self.setMaximumWidth(500)
        self.mid_label = QLabel('发布任务')
        self.mid_label.setAlignment(Qt.AlignCenter)
        self.mid_label.setFont(self.big_font)

        self.title_layout = QHBoxLayout()
        self.title_label = QLabel("标题:")
        self.title_label.setFont(self.label_font)
        self.title_edit = QLineEdit()
        self.title_layout.addWidget(self.title_label)
        self.title_layout.addWidget(self.title_edit)

        self.type_layout = QHBoxLayout()
        self.type_label = QLabel('类型:')
        self.type_label.setFont(self.label_font)
        self.type_comb = QComboBox()
        self.type_comb.setView(QListView())
        self.type_comb.setStyleSheet("QComboBox QAbstractItemView::item{height:40px;}"
                                     "QComboBox{font-size:13px}")
        self.type_comb.addItems(btn_index_map.keys())
        self.type_layout.addWidget(self.type_label)
        self.type_layout.addWidget(self.type_comb)

        self.name_layout = QHBoxLayout()
        self.name_label = QLabel("发布人:")
        self.name_label.setFont(self.label_font)
        self.name_edit = QLineEdit()
        self.name_edit.setEnabled(False)
        self.hid_name_check = QCheckBox('匿名发布')
        self.hid_name_check.setChecked(True)
        self.hid_name_check.stateChanged.connect(self._change_hid)
        self.name_layout.addWidget(self.name_label)
        self.name_layout.addWidget(self.name_edit)
        self.name_layout.addWidget(self.hid_name_check)

        self.duration_layout = QHBoxLayout()
        self.duration_label = QLabel('持续时间:')
        self.duration_label.setFont(self.label_font)
        self.day_comb = QComboBox()
        self.data_label = QLabel('天')
        self.hour_comb = QComboBox()
        self.hour_label = QLabel('小时')
        self.minu_comb = QComboBox()
        self.minu_label = QLabel('分钟')
        self.init_duration()
        self.duration_layout.addWidget(self.duration_label)
        self.duration_layout.addWidget(self.day_comb)
        self.duration_layout.addWidget(self.data_label)
        self.duration_layout.addWidget(self.hour_comb)
        self.duration_layout.addWidget(self.hour_label)
        self.duration_layout.addWidget(self.minu_comb)
        self.duration_layout.addWidget(self.minu_label)

        self.text_layout = QHBoxLayout()
        self.text_label = QLabel('内容:')
        self.text_label.setFont(self.label_font)
        self.text_edit = QTextEdit()
        self.text_layout.addWidget(self.text_label)
        self.text_layout.addWidget(self.text_edit)

        self.btn_layout = QHBoxLayout()
        self.OK_btn = QPushButton('发布')
        self.OK_btn.setStyleSheet(self.btn_style)
        self.OK_btn.clicked.connect(self._publish)
        self.NO_btn = QPushButton('取消')
        self.NO_btn.setStyleSheet(self.btn_style)
        self.NO_btn.clicked.connect(lambda x : self.close())
        self.btn_layout.addWidget(self.OK_btn)
        self.btn_layout.addWidget(self.NO_btn)

        self.main_layout.addWidget(self.mid_label)
        self.main_layout.addLayout(self.title_layout)
        self.main_layout.addLayout(self.type_layout)
        self.main_layout.addLayout(self.name_layout)
        self.main_layout.addLayout(self.duration_layout)
        self.main_layout.addLayout(self.text_layout)
        self.main_layout.addLayout(self.btn_layout)

        self.setLayout(self.main_layout)
        
    def _change_hid(self,status):
        if status == Qt.Checked:
            self.name_edit.setEnabled(False)
        else:
            self.name_edit.setEnabled(True)

    def init_duration(self):
        """
        初始化天数
        :return:
        """
        self.day_comb.addItems([str(d) for d in range(31)])
        self.hour_comb.addItems([str(h) for h in range(24)])
        self.minu_comb.addItems([str(m) for m in range(10,60)])

    def _get_duration_seconds(self):
        """
        获取持续时间
        :return:
        """
        day = int(self.day_comb.currentText())
        hour = int(self.hour_comb.currentText())
        minu = int(self.minu_comb.currentText())

        seconds = minu*60 + hour*60*60 + day*24*60*60

        return seconds


    def _publish(self):

        data = {}
        _title = self.title_edit.text()
        _name = self.name_edit.text() if not self.hid_name_check.isChecked() else "xxx"
        _type = self.btn_index_map[self.type_comb.currentText()]
        _text = self.text_edit.toPlainText()
        _pub_time = int(time.time()) #time.strftime('%Y-%m-%d %H:%M:%S')
        duration = self._get_duration_seconds()
        _end_time = int(_pub_time + duration)

        if _title and _text:
            data['_title'] = _title
            data['_name'] = _name
            data['_type'] = _type
            data['_text'] = _text
            data['_pub_time'] = _pub_time  #  time.strftime('%Y-%m-%d %H:%M:%S')
            data['_end_time'] = _end_time

            result = self.db_hanlder.set_data(data)
            if result:
                QMessageBox.information(None,"成功","发布成功")
                self.close()
            else:
                QMessageBox.information(None,"失败","发布失败")
        else:
            QMessageBox.information(None,"错误","信息不完整")

        print('data:',data)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    btn_map = {0: '合租转租', 1: '宠物', 2: '寻物/失物', 3: '二手交易', 4: '技术交流',
                    5: '游戏开黑', 6: '活动娱乐', 7: '吐槽搞事', 8: '其他'}

    btn_index_map = dict((v, k) for k, v in btn_map.items())
    at = AddWindow(btn_index_map)
    at.show()
    sys.exit(app.exec_())





